#!/bin/bash
echo "Git账户密码登入"
CONUSER='0001100986'
CONPASS='FHlBIuSDpjyQkddYhnjYKNuvzp9L4SfV8YTRvqaP%2FQ'

echo "定义输出目录"
path="/home/ftp_ver/SDWAN/"
nowdate=${path}`date +%Y%m%d`"/packages"
echo ${nowdate}

echo "定义输出文件"
name="uap-web-V16.21.40C4B01-"
sdwanname="uap-sdwan-web-V1.00.10(1.1.1)-"
date=`date +%Y%m%d`
oldname=${name}${date}
filename=${sdwanname}${date}
echo ${filename}

if [ ! -d ${nowdate} ]; then
 echo "创建输出目录"
 mkdir -p ${nowdate}
else
 echo "删除旧安装包 "${oldname}" and "${filename}
 cd ${nowdate} && rm -rf ${oldname}.zip && rm -rf ${filename}.zip && cd /mnt/sdwan/ums-server/webs/ && rm -rf home
fi

echo "开始执行"
cd /var/lib/jenkins/workspace/SDWAN_WEB_Feature/

echo "重置仓库"
rm -rf sdnc_ione

echo "同步远程仓库"
git clone -b develop http://${CONUSER}:${CONPASS}@gerrit.zte.com.cn/a/zxwt_dev/sdnc_ione
if [ $? -ne 0 ]; then
    echo "Error develop分支代码，拉取失败！"
  exit 1
else
	echo "Success develop 分支代码，获取成功！"
	echo "安装依赖包"
	cd /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web && npm install --ignore-scripts && npm fund
	if [ $? -ne 0 ]; then
		echo "Error 依赖包安装失败！"
		exit 1
	else
		echo "Success 代码环境初始化完成！"
		echo "Start 开始构建"
		npm run build && sleep 1
		if [ $? -ne 0 ]; then
			echo "Error WEB构建失败"
			exit 1
		else
			echo "Success WEB构建成功"
      echo "项目文件处理中......"
			cd /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web/dist/ && mkdir resources && cp /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web/dist/index.html /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web/dist/resources
			echo "文件名称定义中......"
      cd /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web/ && mv dist home
      echo "复制项目文件到指定目录"
      cp -r /var/lib/jenkins/workspace/SDWAN_WEB_Feature/sdnc_ione/web/home/ /mnt/sdwan/ums-server/webs/ && cd /mnt/sdwan
      echo "项目文件打包中......"
      zip -r -q -o ${filename}.zip ums-server/ zipPackageName.xml && cp /mnt/sdwan/${filename}.zip ${nowdate} && rm -rf ${filename}.zip
      echo "End 项目构建完成"
			exit 0
		fi
		exit 0
	fi
	exit 0
fi